package com.zjh.flux;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.MultiValueMap;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServer;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;

/**
 * @Auther: zjh
 * @Date: 2025/11/4 - 11 - 04 - 22:13
 * @Description: com.zjh.flux
 * @version: 1.0
 */
public class FluxMainApplication {
    public static void main(String[] args) throws IOException {
        //请求映射
        HttpHandler handler = ( request,  response)->{
            URI uri = request.getURI();
            MultiValueMap<String, String> queryParams = request.getQueryParams();
//            response.bufferFactory() //获取buffer工厂
            DataBuffer dataBuffer = response.bufferFactory().wrap("i an your father".getBytes(StandardCharsets.UTF_8));

            //Publisher<? extends DataBuffer> body需要找个参数

            return  response.writeWith(Mono.just(dataBuffer));
        };
        //启动一个响应式服务器，交给HttpHandler 去处理；
        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
        HttpServer.create().host("localhost").port(8080).handle(adapter).bind().block();
        System.in.read();
    }
}
